package com.xbongbong.saas.service;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.customer.pojo.dto.CustomerRestoreBatchDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainBatchLinkItemDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainInLinkItemDTO;
import com.xbongbong.pro.invoice.pojo.InvoiceCancelOrRestorePojo;
import com.xbongbong.pro.invoice.pojo.InvoiceCommonSavePojo;
import com.xbongbong.pro.invoice.pojo.dto.ComputeInvoiceAmountDTO;
import com.xbongbong.pro.invoice.pojo.dto.CustomerInvoiceAndExpressGetDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceAmountDetailGetDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceDeleteBatchDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceEditAttrUpdateDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceGetByContractDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceUpdateByBusinessRuleDTO;
import com.xbongbong.pro.invoice.pojo.vo.ComputeInvoiceAmountVO;
import com.xbongbong.pro.invoice.pojo.vo.CustomerInvoiceAndExpressGetVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceAmountDetailVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceDeleteBatchVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceGetByCustomerVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceUpdateByBusinessRuleVO;
import com.xbongbong.pro.listbatch.pojo.dto.AddLabelDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 发票业务处理
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/22
 * @since v1.0
 */
public interface InvoiceService {
    /**
     * 发票保存前saas特殊校验
     *
     * @param validateDataDTO saas特殊校验数据入参
     * @throws XbbException 业务异常
     * @author feng.zheng
     * @date 2019/1/18
     */
    void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException;

    /**
     * 发票保存后逻辑
     *
     * @param saasFormSaveDTO saas保存数据
     * @throws XbbException 业务异常
     * @author feng.zheng
     */
    void afterSave(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;



    /**
     * 根据规则批量更新
     * @author 吴峰
     * @date 2019/2/17 15:51
     * @param invoiceUpdateByBusinessRuleDTO
     * @return UpdateInvoiceByBusinessRuleVO
     * @throws XbbException
     */
    InvoiceUpdateByBusinessRuleVO updateFormDataByBusinessRule(InvoiceUpdateByBusinessRuleDTO invoiceUpdateByBusinessRuleDTO) throws XbbException;

    /**
     * 合并开票
     *
     * @param corpid  公司id
     * @param invoiceId  发票id
     * @param invoiceMoney 开票金额
     * @param relationIdIn 传参
     * @param isImport 是否导入
     * @param amountDetail 金额明细
     * @author feng.zheng
     * @throws XbbException 异常
     * @date 2019/2/26 18:44
     */
    void invoiceAmountJudgment(String corpid, Long invoiceId, Double invoiceMoney, Map<String, Object> relationIdIn, Integer isImport,Integer isCover,Integer isRestore,JSONArray amountDetail) throws XbbException;


    /**
     * 获取relationMap来判断金额
     * @param isHasPaymentSheet 是否有回款单
     * @param isPrePaymentSheet 是否是预收款回款单
     * @param isHasPayment 是否有应收款
     * @param corpid       公司id
     * @param contractIds  合同集合
     * @param paymentOrSheetIds   回款集合
     * @return relationMap
     *  @throws XbbException 异常
     * @author feng.zheng
     * @date 2019/2/27 11:10
     */
    Map<String, Object> getRelationMapForJudgeMoney(boolean isHasPaymentSheet,boolean isPrePaymentSheet,boolean isPrePaymentSheetUnion,boolean isHasPayment, String corpid, List<Long> contractIds, List<Long> paymentOrSheetIds) throws XbbException;

    /**
     * 计算开票金额
     * @param computeInvoiceAmountDTO 计算开票金额DTO
     * @return
     * @throws XbbException
     */
    ComputeInvoiceAmountVO getComputeInvoiceAmount(ComputeInvoiceAmountDTO computeInvoiceAmountDTO)throws XbbException;

    /**
     * 获取发票信息和寄送信息
     *
     * @param customerInvoiceAndExpressGetDTO 入参
     * @return 回参
     * @throws XbbException 异常
     */
    CustomerInvoiceAndExpressGetVO getCustomerInvoiceAndExpress(CustomerInvoiceAndExpressGetDTO customerInvoiceAndExpressGetDTO) throws XbbException;

    /**
     * 批量删除
     * @param invoiceDeleteBatchDTO
     * @param delEnum
     * @return
     * @throws XbbException
     */
    InvoiceDeleteBatchVO deleteBatch(InvoiceDeleteBatchDTO invoiceDeleteBatchDTO, DelEnum delEnum) throws XbbException;

    /**
     * 格式化移动端列表页
     *
     * @param formDataListDTO 用户
     * @param esEntities 业务实体集合
     * @param totalCount 总数
     * @return ListAppVO
     * @throws XbbException
     * @author long.rao
     * @date 2019-03-11 13:50
     */
    ListAppVO formatInvoiceAppList(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt> esEntities, Integer totalCount) throws XbbException;

    /**
     * 关联新建时格式化解释
     *
     * @param handlerExplainInLinkItemDTO 入参
     * @throws XbbException 异常
     * @author feng.zheng
     * @date 2019-03-16 20:53
     */
    void formatLinkAddExplain(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException;

    /**
     * 批量编辑某个字段
     * @author 吴峰
     * @date 2019/3/20 15:35
     * @param invoiceEditAttrUpdateDTO
     * @return void
     * @throws XbbException 业务异常
     */
    void batchEditAttr(InvoiceEditAttrUpdateDTO invoiceEditAttrUpdateDTO) throws XbbException;

    /**
     * 合并开票
     *
     * @param handlerExplainBatchLinkItemDTO 入参
     * @throws XbbException 异常
     */
    void formatBatchLinkAddExplain(HandlerExplainBatchLinkItemDTO handlerExplainBatchLinkItemDTO) throws XbbException;

    /**
     * 新建时格式化解释
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     */
    void formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException;

    /**
     * 编辑时获取红冲总金额
     * @param originId
     * @param corpid
     * @param editInvoiceId
     * @return
     * @throws XbbException
     */
    Double getRedTotalMoneyByEdit(Long originId, String corpid,Long editInvoiceId) throws XbbException;

    /**
     * 根据关联id 获取红冲金额
     * @param originId
     * @param corpid
     * @return
     * @throws XbbException
     */
    Map<Long, Double> getRedTotalMoneyByRef(Long originId, String corpid, Long refId, XbbRefTypeEnum xbbRefTypeEnum) throws XbbException;

    /**
     * 批量获取红冲总金额
     *
     * @param originIdIn 原始id
     * @param negIdIn 排除发票id
     * @param corpid 公司id
     * @return double
     * @throws XbbException 异常
     */
    Map<Long, Double> getRedTotalMoneyByIdIn(List<Long> originIdIn,List<Long> negIdIn, String corpid) throws XbbException;


    /**
     * 批量获取红冲总金额
     *
     * @param originIdIn 原始id
     * @param corpid 公司id
     * @return double
     * @throws XbbException 异常
     */
    Map<Long,Long> getInvoiceRedIdMappingByIdIn(List<Long> originIdIn, String corpid) throws XbbException;

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO 导入入参
     * @param titlePojoList     标题
     * @param rowGroupPojo      一个表单对应的一组Excel填写的数据
     * @param fieldAttrList     解释
     * @return FormDataAddDTO
     * @throws XbbException XbbException
     * @author feng.zheng
     * @date 2019/5/20 14:16
     * @since V1.0
     */
    FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 格式化导出值
     *
     * @param paasFormDataESList 数据
     * @throws XbbException 异常
     */
    void formatExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO, List<FieldAttrEntityForImport> fieldAttrList);

    /**
     * 发票作废还原删除都可用此接口更新其他业务发票金额
     *
     * @param corpid 公司id
     * @param invoiceCancelOrRestorePojo 发票作废或还原的pojo
     * @param isCancel 作废，还原为0
     * @param isDelete 删除为1
     * @param businessType 业务类型
     * @throws XbbException 异常
     */
    void updateInvoiceMoney(String corpid, InvoiceCancelOrRestorePojo invoiceCancelOrRestorePojo, Integer isCancel,Integer isDelete,Integer businessType) throws XbbException;

    /**
     * Description: 一致性同步
     * @param corpid
     * @param customerId
     * @param customerName
     * @return void
     * @author 魏荣杰
     * @date 2019/6/11 19:14
     * @since v1.0
     */
    void dataConsistencyUpdate(String corpid, Long customerId, String customerName, String attr, String nameAttr) throws XbbException;

    /**
     * 更新回滚
     * @param idIn
     * @param corpid
     * @throws XbbException
     */
    void updateRollback(List<Long> idIn, String corpid) throws XbbException;

    /**
     * 还原数据
     * @author xingxing.xiao
     */
    void restoreBatch(String corpid, Map<Long, Long> customerUpdateTimeMap, CustomerRestoreBatchDTO customerRestoreBatchDTO) throws XbbException;

    /**
     * 根据客户ID查询发票列表
     * @param invoiceGetByContractDTO 入参
     * @return 回参
     * @throws XbbException error
     * @author zhouwq
     * @date 2019/11/20 18:52
     */
    InvoiceGetByCustomerVO getByContract(InvoiceGetByContractDTO invoiceGetByContractDTO) throws XbbException;


    void checkBusinessDeleteHaveInvoice(List<Long> idList, String corpid, XbbRefTypeEnum xbbRefTypeEnum, Collection<String> errorTypeSet, Collection<String> errorDataSet, Collection<Long> delIdList, Collection<String> delNoList,Map<String,Long> parentMap,Map<Long,String> parentSerialNo,Map<Long,String> subMap,Map<String,List<Long>> uuidSubMap,Map<String,List<String>> uuidSubSerialNoMap) throws XbbException;

    /**
     * 一致性同步
     * @param corpid 公司ID
     * @param contractId 合同id
     * @param serialNo 合同编号
     * @throws XbbException
     * @author long.rao
     * @date 2020-05-08 15:03
     * @since v1.0
     * @version v1.0
     */
    void dataConsistencyUpdateContract(String corpid, Long contractId, String serialNo) throws XbbException;

    /**
     * 销项发票管理应收款一致性同步
     * @param corpid　公司ID
     * @param paymentId 付款计划Id
     * @param serialNo 付款计划 编号
     * @throws XbbException
     * @author yuqian
     * @date 2020-05-11 15:24
     * @since v1.0
     * @version v1.0
     */
   void dataConsistencyUpdatePayment(String corpid,Long paymentId,String serialNo) throws XbbException;

    /**
     * 获取销项发票金额明细
     * @param purchaseInvoiceAmountDetailGetDTO
     * @return
     */
    InvoiceAmountDetailVO getInvoiceAmountDetail(InvoiceAmountDetailGetDTO purchaseInvoiceAmountDetailGetDTO) throws XbbException;

    /**
     * 批量增删标签
     * @param addLabelDTO
     * @param addBatchList
     * @throws XbbException
     */
    void updateBatchLabelAttr(AddLabelDTO addLabelDTO, List<PaasFormDataEntityExt> addBatchList, Boolean addOrDelete) throws XbbException;

    void afterSaveForWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    void afterSaveForUpdateWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    void invoiceCommonSaveHandle(InvoiceCommonSavePojo invoiceCommonSavePojo)throws XbbException;
}
